<?php

namespace app\common\model;

use app\common\model\BaseModel as BaseModel;
use ShidianUtil\Thinkphp\Relation\MorphTo;

/**
 * Class AccountBill
 * @package app\common\model
 * @property int $id
 * @property int $proxyUserPersonnelId
 * @property int $proxyMemberId
 *
 * @property string $fromType
 * @property int $fromMemberId
 * @property string $fromAccountNumber
 * @property float $fromBeforeMoney
 * @property float $fromAfterMoney
 * @property string $toType
 * @property int $toMemberId
 * @property string $toAccountNumber
 * @property float $toBeforeMoney
 * @property float $toAfterMoney
 *
 * @property string $accountType
 * @property float $money
 * @property string $type
 * @property string $billType
 * @property string $desc
 * @property int $tradeId
 * @property int $transferId
 * @property int $cashId
 * @property int $sourceId
 * @property int $updated
 * @property int $created
 * @link https://www.kancloud.cn/manual/thinkphp6_0/1037579
 * @link https://www.kancloud.cn/manual/think-orm/1257998
 * @link https://www.kancloud.cn/thinkphp/master-database-and-model/265551
 */
class AccountBill extends BaseModel
{
    const MEMBER = 'member';
    const SYSTEM = 'system';
    const COMPANY = 'company';

    protected $name = 'AccountBill';

    // 付款人
    public function from()
    {
        $alias = [
            'member' => Member::class,
            'system' => Platform::class,
            'company' => User::class,
        ];
        return new MorphTo($this, 'fromType', 'fromMemberId', $alias, 'from');
    }

    // 收款人
    public function to()
    {
        $alias = [
            'member' => Member::class,
            'system' => Platform::class,
            'company' => User::class,
        ];
        return new MorphTo($this, 'toType', 'toMemberId', $alias, 'to');
    }

    // 订单
    public function trade()
    {
        return $this->belongsTo(Trade::class, 'tradeId', 'id');
    }

    // 转账
    public function transfer()
    {
        return $this->belongsTo(Transfer::class, 'transferId', 'id');
    }

    public function proxyUserPersonnel()
    {
        return $this->hasOne(UserPersonnel::class, 'id', 'proxyUserPersonnelId');
    }

    public function proxyMember()
    {
        return $this->hasOne(Member::class, 'id', 'proxyMemberId');
    }

    // 前端账单图标
    public function getIconImgAttr($value, $data)
    {
        //  pay支付(trade) refund退款(trade) transfer转账
        $type = $data['type'];
        $billType = $data['billType'];

        if ($type == 'pay') {
            // 付款
            if ($billType == TRADE_LAS_CASE || $billType == TRADE_SOURCE) {
                return image_url('/storage/static/images/bill/anyuan.png');
            }
            if ($billType == TRADE_CONSULTING_SERVER) {
                return image_url('/storage/static/images/bill/zixun.png');
            }
            return image_url('/storage/static/images/bill/zhichu.png');
        }

        if ($type == 'refund') {
            // 退款
            if ($billType == TRADE_LAS_CASE || $billType == TRADE_SOURCE) {
                return image_url('/storage/static/images/bill/anyuan.png');
            }
            if ($billType == TRADE_CONSULTING_SERVER) {
                return image_url('/storage/static/images/bill/zixun.png');
            }
            return image_url('/storage/static/images/bill/shouru.png');
        }

        if ($type == 'transfer') {
            // 转账

            return image_url('/storage/static/images/bill/shouru.png');
        }

        // /storage/static/images/bill
    }

    /**
     * 1对1
     * @return \think\model\relation\HasOne
     */
    //    public function one(){
    //        return $this->hasOne( Test1::class, 'Test1表中的键', 'AccountBill表中的键');
    //    }

    /**
     * 1对多
     * @return \think\model\relation\HasMany
     */
    //    public function many(){
    //        return $this->hasMany( Test1::class, 'Test1表中的键', 'AccountBill表中的键');
    //    }

    /**
     * 多对多
     * @return \think\model\relation\BelongsToMany
     */
    //    public function belongs(){
    //        return $this->belongsToMany( Test1::class, '中间表', '中间表中Test1的键', '中间表中AccountBill的键');
    //    }
}
